package com.daigoudao.web.action;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.daigoudao.config.ConfigManager;
import com.daigoudao.util.FileUtil;
import com.daigoudao.util.ImageUtil;
import com.daigoudao.util.StringUtils;
import com.daigoudao.web.common.BasePageAction;
import com.daigoudao.web.model.AreaModel;
import com.daigoudao.web.model.CategoryModel;
import com.daigoudao.web.model.SimpleBuyModel;
import com.daigoudao.web.model.UserModel;
import com.daigoudao.web.model.WantBuyModel;
import com.daigoudao.web.service.ICommonService;
import com.daigoudao.web.service.ISimpleBuyService;
import com.daigoudao.web.service.IWantBuyService;

public class WantBuyAction extends BasePageAction {

	private static final long serialVersionUID = -453607143090802698L;

	private static Logger logger = LoggerFactory.getLogger(WantBuyAction.class);

	private IWantBuyService wantBuyService;

	private ICommonService commonService;

	private ISimpleBuyService simpleBuyService;

	private List<CategoryModel> categorys;

	private List<AreaModel> areas;

	private WantBuyModel wantBuyModel;

	private List<WantBuyModel> wantBuyModels;

	private SimpleBuyModel simpleBuyModel;

	private List<SimpleBuyModel> simpleBuyModels;

	/**
	 * 同区域其他求购信息
	 */
	private List<WantBuyModel> otherWantBuysOfArea;

	/**
	 * 同类型其他求购信息
	 */
	private List<WantBuyModel> otherWantBuysOfCategory;

	/**
	 * 上传的文件
	 */
	private File image;

	/**
	 * 上传的文件名称
	 */
	private String imageFileName;

	protected String ajaxMsg;

	/**
	 * 初始化发布求购信息页面
	 * 
	 * @return
	 */
	public String initAddWantBuy() {
		if (!isLogin()) {
			return LOGIN;
		}

		try {
			if (id != 0) {
				wantBuyModel = wantBuyService.queryWantBuyDetail(id);
			}
			categorys = commonService.queryCategorys();
			areas = commonService.queryAreas();
		} catch (Exception e) {
			logger.error("initAddWantBuy error.", e);
			return ERROR;
		}
		return SUCCESS;
	}

	/**
	 * 发布求购信息
	 * 
	 * @return
	 */
	public String addWantBuy() {
		try {
			if (image != null) {
				String root = ConfigManager.getConfigString("upload.resDir");
				String path = StringUtils.formatDate(new Date(), "yyyy-MM-dd");
				String basePath = root + File.separator + path;
				FileUtil.createDir(basePath);
				String fileName = imageFileName;
				String extName = FileUtil.getExtention(fileName);

				fileName = StringUtils.getUUID() + extName;
				File destFile = new File(basePath, fileName);

				// 文件复制
				try {
					FileUtil.copy(image, destFile);
				} catch (Exception e) {
				}

				// 处理文件
				try {
					ImageUtil.resize50(destFile.getPath());
					ImageUtil.resize200(destFile.getPath());
				} catch (IOException e) {
					logger.error("ImageUtil resize120 ERROR", e);
				}

				// 文件在数据库存储
				String urlPath = File.separator + path + File.separator
						+ fileName;
				wantBuyModel.setImage(urlPath);
			}

			wantBuyService.addWantBuy(wantBuyModel);
		} catch (Exception e) {
			logger.error("addWantBuy error.", e);
			return ERROR;
		}
		return SUCCESS;
	}

	/**
	 * 显示求购信息列表
	 * 
	 * @return
	 */
	public String showWantBuys() {
		try {
			areas = commonService.queryAreas();
			categorys = commonService.queryCategorys();
			areas.add(0, new AreaModel(0, "不限"));
			categorys.add(0, new CategoryModel(0, "不限"));
			WantBuyModel query = new WantBuyModel();

			if (0 != id) {
				query.setAreaId(id);
			}
			query.setGoodsName(getQ());
			query.setCategoryId(getCategory());
			query.setAreaId(getArea());
			query.setOrder(getO());
			UserModel userModel = getUserLogin();
			if (userModel != null) {
				query.getUser().setUserid(userModel.getUserid());
			}
			query.setStartIndex((getSuperPage() - 1) * getPageSize());
			query.setPageSize(getPageSize());
			if (getPubtime() > 0) {
				query.setTs(processPubtime());
			}
			wantBuyModels = wantBuyService.queryWantBuys(query);
			setTotalSize(wantBuyService.getWantBuyCount(query));
		} catch (Exception e) {
			logger.error("queryWantBuys error.", e);
			return ERROR;
		}
		if (1 == requestType) {
			return AJAX;
		}
		return SUCCESS;
	}

	/**
	 * 查询求购详情
	 * 
	 * @return
	 */
	public String queryWantBuyDetail() {
		if (0 != id) {
			wantBuyModel = new WantBuyModel();
			wantBuyModel.setWantId(id);
		}
		try {
			wantBuyModel = wantBuyService.queryWantBuyDetail(wantBuyModel
					.getWantId());
			WantBuyModel queryModel = new WantBuyModel();
			queryModel.setWantId(wantBuyModel.getWantId());
			queryModel.setCategoryId(wantBuyModel.getCategoryId());
			otherWantBuysOfCategory = wantBuyService
					.queryOtherWantBuys(queryModel);
			queryModel.setCategoryId(0);
			queryModel.setAreaId(wantBuyModel.getAreaId());
			otherWantBuysOfArea = wantBuyService.queryOtherWantBuys(queryModel);
		} catch (Exception e) {
			logger.error("queryWantBuyDetail error.", e);
			return ERROR;
		}
		return SUCCESS;
	}

	/**
	 * 搭车求购
	 * 
	 * @return
	 */
	public String rideBuy() {
		if (0 != id) {
			wantBuyModel = new WantBuyModel();
			wantBuyModel.setWantId(id);
		}
		try {
			wantBuyModel = wantBuyService.queryWantBuyDetail(wantBuyModel
					.getWantId());
		} catch (Exception e) {
			logger.error("rideBuy error.", e);
			return ERROR;
		}
		return SUCCESS;
	}

	/**
	 * 确认搭车求购
	 * 
	 * @return
	 */
	public String confirmRideBuy() {
		UserModel userModel = getUserLogin();
		if (userModel == null) {
			return LOGIN;
		}
		try {
			simpleBuyModel.getUser().setUserid(userModel.getUserid());
			simpleBuyService.addSimpleBuy(simpleBuyModel);

		} catch (Exception e) {
			logger.error("confirmRideBuy error.", e);
			return ERROR;
		}
		return SUCCESS;
	}

	/**
	 * 查询搭车求购记录
	 * 
	 * @return
	 */
	public String querySimpleBuys() {
		try {
			simpleBuyModels = simpleBuyService
					.querySimpleBuyModels(simpleBuyModel);
		} catch (Exception e) {
			logger.error("querySimpleBuys error.", e);
			return ERROR;
		}
		return SUCCESS;
	}

	/**
	 * 判断能否进行搭车求购
	 * 
	 * @return
	 */
	public String canRideBuy() {
		simpleBuyModel.getUser().setUserid(getUserLogin().getUserid());
		try {
			if (simpleBuyService.isRideBuy(simpleBuyModel)) {
				ajaxMsg = "您已经搭车求购过了，不能重复搭车。";
			}
		} catch (Exception e) {
			logger.error("canRideBuy error.", e);
			ajaxMsg = "系统忙，请稍后再试。";
		}
		return SUCCESS;
	}

	public String pauseWantBuy() {
		WantBuyModel wantBuy = new WantBuyModel();
		wantBuy.setStatus(4);
		wantBuy.setWantId(id);
		try {
			wantBuyService.updateStatus(wantBuy);
		} catch (Exception e) {
			logger.error("pauseWantBuy error.", e);
			return ERROR;
		}
		return SUCCESS;
	}

	public String resumeWantBuy() {
		WantBuyModel wantBuy = new WantBuyModel();
		wantBuy.setStatus(0);
		wantBuy.setWantId(id);
		try {
			wantBuyService.updateStatus(wantBuy);
		} catch (Exception e) {
			logger.error("resumeWantBuy error.", e);
			return ERROR;
		}
		return SUCCESS;
	}

	public List<WantBuyModel> getWantBuyModels() {
		return wantBuyModels;
	}

	public void setWantBuyModels(List<WantBuyModel> wantBuyModels) {
		this.wantBuyModels = wantBuyModels;
	}

	public File getImage() {
		return image;
	}

	public void setImage(File image) {
		this.image = image;
	}

	public String getImageFileName() {
		return imageFileName;
	}

	public void setImageFileName(String imageFileName) {
		this.imageFileName = imageFileName;
	}

	public ICommonService getCommonService() {
		return commonService;
	}

	public void setCommonService(ICommonService commonService) {
		this.commonService = commonService;
	}

	public void setWantBuyService(IWantBuyService wantBuyService) {
		this.wantBuyService = wantBuyService;
	}

	public List<CategoryModel> getCategorys() {
		return categorys;
	}

	public void setCategorys(List<CategoryModel> categorys) {
		this.categorys = categorys;
	}

	public List<AreaModel> getAreas() {
		return areas;
	}

	public void setAreas(List<AreaModel> areas) {
		this.areas = areas;
	}

	public WantBuyModel getWantBuyModel() {
		return wantBuyModel;
	}

	public void setWantBuyModel(WantBuyModel wantBuyModel) {
		this.wantBuyModel = wantBuyModel;
	}

	public SimpleBuyModel getSimpleBuyModel() {
		return simpleBuyModel;
	}

	public void setSimpleBuyModel(SimpleBuyModel simpleBuyModel) {
		this.simpleBuyModel = simpleBuyModel;
	}

	public void setSimpleBuyService(ISimpleBuyService simpleBuyService) {
		this.simpleBuyService = simpleBuyService;
	}

	public List<SimpleBuyModel> getSimpleBuyModels() {
		return simpleBuyModels;
	}

	public void setSimpleBuyModels(List<SimpleBuyModel> simpleBuyModels) {
		this.simpleBuyModels = simpleBuyModels;
	}

	public List<WantBuyModel> getOtherWantBuysOfArea() {
		return otherWantBuysOfArea;
	}

	public void setOtherWantBuysOfArea(List<WantBuyModel> otherWantBuysOfArea) {
		this.otherWantBuysOfArea = otherWantBuysOfArea;
	}

	public List<WantBuyModel> getOtherWantBuysOfCategory() {
		return otherWantBuysOfCategory;
	}

	public void setOtherWantBuysOfCategory(
			List<WantBuyModel> otherWantBuysOfCategory) {
		this.otherWantBuysOfCategory = otherWantBuysOfCategory;
	}

	public String getAjaxMsg() {
		return ajaxMsg;
	}

	public void setAjaxMsg(String ajaxMsg) {
		this.ajaxMsg = ajaxMsg;
	}

}
